<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Listing/classes/PhpFox_ComponentListingCreate.class.php, $Author:  $ */



/** Display list classifieds
 *
 * Parameters:
 *  1. oUser (mandatory) - user account object
 *
 * @package module.listing
 * @version $Id:
 *   PHPFox Team ()
 */
class PhpFox_Mod_Listing_ComponentListingCreate extends BaseComponent
{
	function process()
	{
		App::membersOnly();

	    //check permissions
        $oSecurityService = App::getModuleService('Account', 'Security');

        $oSecurityService->checkPerm('listing', 'full');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
		$oListingService = $this->_oModule->getService('Listing');
		$bOwner = false;
		$bAdmin = false;
        /* @var $oGroupsService PhpFox_Mod_Groups_ServiceGroups */

        $oReq = &App::getRequest();
        /* @var $oReq Request */
        // Validation
        $aFields = array(
							'aList[title]'   => array('title'=>App::format('Mod_Listing.subject'), 'def'=>'required'),
							'aList[city]'    => array('title'=>App::format('Mod_Listing.city'), 'def'=>'required'),
                            'aList[text]'    => array('title'=>App::format('Mod_Listing.description'), 'def'=>'required'),
                            );
        $oValidator = &App::newObject('Validator', $aFields);
        $oUser = $oSecurityService->getCurrentUser();
        $aList['location'] = $oUser->aData['location'];
        $aList['city']     = $oUser->aData['city'];
        if ( App::getParam('us_states') )
        {
        	$aList['state']    = $oUser->aData['state'];
        }

        if($oReq->getInt('id'))
        {
            $aList = $oListingService->getListingById($oReq->getInt('id'));
            $bOwner = ($oUser->aData['user'] == $aList['user']) ? true : false;
            $bAdmin = $oSecurityService->isAdmin() ? true : false;

            if(!$bAdmin && !$bOwner)
                App::gotoUrl('public.listing');
        }

        //action block
        if($oReq->getAction() == 'btnCreate')
        {
            $aPost = $oReq->getArray('aList');
            if($oValidator->isValid($oReq->getAll()))
            {
                if($oReq->getInt('id'))
                {
                    if(!$bOwner)
                        App::gotoUrl('public.listing');

                    if($oListingService->checkListingName($aPost['title'], intval($aPost['category']), $oReq->getInt('id')))
                    {
                        $this->addErrors(App::format('Mod_Listing.same.name'));
                        $aList = $aPost;
                    }
                    else
                    {
                        $oListingService->updateListing($aPost, $oUser->aData['user'], $oReq->getInt('id'));
                        $iRedirId = $oReq->getInt('id');
                        $mMess = App::format('Mod_Listing.listing.update');
                    }
                }
                else
                {
                    if($oListingService->checkListingName($aPost['title'], intval($aPost['category'])))
                    {
                        $this->addErrors(App::format('Mod_Listing.same.name'));
                        $aList = $aPost;
                    }
                    else
                    {
	                    $iRedirId = $oListingService->addListing($aPost, $oUser->aData['user']);
	                    $mMess = App::format('Mod_Listing.listing.add');
                    }
                }
                $aErrors = $this->getErrors();
                if(empty($aErrors))
                    App::gotoUrl('public.listing.view', array('id'=>$iRedirId), array($mMess), false, 'View');
            }
            else
            {
                $aList = $aPost;
                $this->addErrors($oValidator->getErrors());
            }
        }
        $aCatHash = $oListingService->getCategoryHash();

        foreach(App::getTxtFile('country.txt') as $i=>$sCountry)
        {
	        $aCountry[$sCountry] = $sCountry;
		}
        foreach(App::getTxtFile('us_stat.txt') as $i=>$sState)
        {
	        $aState[$sState] = $sState;
		}
        $oTpl = $this->_oModule->getTemplate();

        $oTpl->assign(array(
                             'aCountry'   => $aCountry,
                             'aState'     => $aState,
                             'bIsEdit'    => $oReq->getInt('id') ? true : false,
        ));

        $oTpl->assignSrc(array(
                               'aCatHash'     => $aCatHash,
                               'iCurentCat'   => !empty($aPost) ? $aPost['category'] : (isset($aList['list_id']) ? $aList['list_id'] : $oReq->getInt('cat')),
                               'jsValidation' => $oValidator->makeJS(),
		                       'jsValMess'    => $oValidator->makeJsMess(),
                               'aErrors'      => $this->getErrors(),
                               'aList'        => !empty($aList) ? $aList : '',
                         ));

        $srcHtml = $oTpl->fetch('ListingCreate.html');
		return $srcHtml;
	}
}
?>